Page({
  data: {
    dialogVisible: false,
    houseList: [],
    id: ''
  },

  async onLoad () {
    await wx.$http.get('/room')
    // console.log(res)
  },
  onShow () {
    this.getHouseList()
  },
  async getHouseList () {
    const res = await wx.$http.get('/room')
    if (res.code !== 10000) return wx.$utils.toast('获取房屋失败')
    this.setData({ houseList: res.data })
    // console.log(res)
  },
  async dialogClose (e) {
    if (e.detail === 'confirm') {
      // console.log(this.data.id)
      const res = await wx.$http.delete(`/room/${this.data.id}`)
      if (res.code !== 10000) return wx.$utils.toast('删除失败')
      // console.log(res)
      wx.$utils.toast('删除成功')
      this.setData({
        houseList: this.data.houseList.filter(item => item.id !== this.data.id)
      })
    }
  },
  swipeClose (ev) {
    const { position, instance } = ev.detail

    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
        id: ev.mark.id
      })

      // swiper-cell 滑块关闭
      instance.close()
    }
  },

  goDetail (e) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + e.mark.id
    })
  },

  addHouse () {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index'
    })
  }
})
